/**
 * 限时折扣
 * */


(function () {
})();


//列表
function getList(){
    var formData = {
        start: pages.start,
        limit: pages.limit
    };
    $.ajax({
        type: apis.erqiCheapIndex.method,
        url: apis.erqiCheapIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                //msgPop({msg: res.msg});
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formArys = res.data;
            tbody.html(getHtml());
            $('#pageTotal').html(formArys.length);
            //pages.total = res.total;
            //pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}
//拼接字符串
function getHtml(){
    var d = formArys;
    var h = '', i, status;
    for (i = 0; i < d.length; i ++) {
        status = d[i].status;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].gname + '</td>';
        h += '<td>' + d[i].per + '</td>';
        h += '<td>' + d[i].period + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td class="f-14">';
        //h += '<a href="javascript:;" onclick="sucai_show(\'详情\',\'sucai-show.html\', ' + d[i].id + ');">';
        //h += '详情';
        //h += '</a>';
        //h += ' &nbsp; ';
        //h += '<a href="javascript:;" onclick="sucai_edit(\'编辑\',\'sucai-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
        //h += '编辑<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="list_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
        h += '删除<i class="Hui-iconfont">&#xe6e2;</i>';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑
function list_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//保存更新
function modify(){
    var gno = $('#gno').val();
    var per = $('#per').val();
    var start_time = $('#start_time').val();
    var end_time = $('#end_time').val();
    var formData = {
        gno: gno, per: per,
        start_time: start_time,
        end_time: end_time,
    };
    if (!gno) {
        msgPop({msg: '商品编号必须的'}); return
    }
    if (!per) {
        msgPop({msg: '折扣率必须的'}); return
    }
    console.log(formData);
    //return
    $.ajax({
        type: apis.erqiCheapModify.method,
        url: apis.erqiCheapModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//删除
function list_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        $.ajax({
            type: apis.erqiCheapDel.method,
            url: apis.erqiCheapDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}